package com.cmd.driver;


import com.alibaba.fastjson.JSONObject;
import com.mgr.WithDrawMgr;
import com.player.DriverUser;
import com.player.User;
import com.qy.bean.DriverInfo;
import com.qy.util.StaticData;
import com.util.AbstractCommand;
import com.util.PBMessage;

/**
 * 
 * 申请提现
 * @author zwl
 * 2018年2月1日
 */
public class WithdrawApplyCMD  extends AbstractCommand{

	@Override
	public void execute(User user, PBMessage packet) throws Exception {
		/*
		 * 1.验证金额是否符合要求
		 * 2.
		 */
	    	DriverInfo info = ((DriverUser)user).getDriverInfo();
		JSONObject reJs = new JSONObject();
		reJs.put(StaticData.reKey.cmd.getKey(),packet.getCode());
		JSONObject param = packet.getMessage();
		if(!param.containsKey("amount")){
			reJs.put(StaticData.reKey.code.getKey(), StaticData.codeType.param_error.getType());
			user.sendPacket(reJs);
			return;
		}
		if(info.getBankCar()==null||info.getBankName()==null){
			reJs.put(StaticData.reKey.code.getKey(), StaticData.codeType.BANK_ERROR.getType());
			user.sendPacket(reJs);
			return;
		}
		//提现金额
		int amount = param.getIntValue("amount");
		
		synchronized (this){
		    if(info.getMoney()-amount<0){
			reJs.put(StaticData.reKey.code.getKey(), StaticData.codeType.WITHDRAW_NOT_ENOUGH.getType());
			user.sendPacket(reJs);
			return;
		    }
		    WithDrawMgr.create(info.getId(), amount);
		    info.setMoney(info.getMoney()-amount);
		}

		reJs.put(StaticData.reKey.code.getKey(), StaticData.codeType.ok.getType());
		user.sendPacket(reJs);
		return;
	}

}
